package com.prestamomo.vil.ppa.pmvilpage.pmcamera;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;

import java.io.IOException;

public final class PmvilTools {

    private PmvilTools() {
        throw new AssertionError();
    }

    private static DisplayMetrics getDisplayPmvilMetrics(Context pmvilCxt) {
        return pmvilCxt.getResources().getDisplayMetrics();
    }

    public static int getScreenPmvilWidth(Context pmvilCext) {
        return getDisplayPmvilMetrics(pmvilCext).widthPixels;
    }

    public static int getScreenPmvilHeight(Context pmvilCntext) {
        return getDisplayPmvilMetrics(pmvilCntext).heightPixels;
    }

    public static int dpPmvil2px(Context pmvilCntext, float dipPmvilValue) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dipPmvilValue, getDisplayPmvilMetrics(pmvilCntext));
    }


    public static int[] getViewPmvilLocal(View pmvilview) {
        int[] outPmvilLocation = new int[2];
        pmvilview.getLocationInWindow(outPmvilLocation);
        return outPmvilLocation;
    }


    public static Matrix picturePmvilDegree(String imgPmvilPath) {
        Matrix pmvilMatrix = new Matrix();
        ExifInterface pmvilExif = null;
        try {
            pmvilExif = new ExifInterface(imgPmvilPath);
        } catch (IOException pmvile) {
            pmvile.printStackTrace();
        }
        if (pmvilExif == null)
            return pmvilMatrix;
        int degpmvilree = 0;
        int orientation = pmvilExif.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1);
        switch (orientation) {
            case ExifInterface.ORIENTATION_ROTATE_90:
                degpmvilree = 90;
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                degpmvilree = 180;
                break;
            case ExifInterface.ORIENTATION_ROTATE_270:
                degpmvilree = 270;
                break;
            default:
                break;
        }
        pmvilMatrix.postRotate(degpmvilree);
        return pmvilMatrix;
    }


    public static Bitmap bitmapPmvilClip(String imgPmvilPath) {
        Bitmap bitPmvilmap = BitmapFactory.decodeFile(imgPmvilPath);
        Log.e("wld__________bitmap", "width:" + bitPmvilmap.getWidth() + "--->height:" + bitPmvilmap.getHeight());
        Matrix matrixPmvil = picturePmvilDegree(imgPmvilPath);
        bitPmvilmap = Bitmap.createBitmap(bitPmvilmap, 0, 0, bitPmvilmap.getWidth(), bitPmvilmap.getHeight(), matrixPmvil, true);
        return bitPmvilmap;
    }


    public static Bitmap landscapePmvilBitmapClip(Context pmvliCext, String imgPmvilPath) {
        Bitmap pmvilBitmap = BitmapFactory.decodeFile(imgPmvilPath);
        Log.d("wld__________bitmap", "width:" + pmvilBitmap.getWidth() + "--->height:" + pmvilBitmap.getHeight());
        Matrix matpmvilrix = picturePmvilDegree(imgPmvilPath);
        double bitmapPmvilRatio = pmvilBitmap.getHeight() * 1. / pmvilBitmap.getWidth();//16/9
        int pmvilWidth = getScreenPmvilWidth(pmvliCext);
        int pmvilHeight = getScreenPmvilHeight(pmvliCext);
        double screenPmvilRatio = pmvilHeight * 1. / pmvilWidth;
        if (bitmapPmvilRatio > screenPmvilRatio) {
            int clipHeight = (int) (pmvilBitmap.getWidth() * screenPmvilRatio);
            pmvilBitmap = Bitmap.createBitmap(pmvilBitmap, 0, (pmvilBitmap.getHeight() - clipHeight) >> 1, pmvilBitmap.getWidth(), clipHeight, matpmvilrix, true);
        } else {
            pmvilBitmap = Bitmap.createBitmap(pmvilBitmap, 0, 0, pmvilBitmap.getWidth(), pmvilBitmap.getHeight(), matpmvilrix, true);
        }
        return pmvilBitmap;
    }


}
